package or;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class T0532 {

    public int findPairs(int[] nums, int k) {

        Arrays.sort(nums);

        int i = 0, j = 1, sum = 0;
        int len = nums.length;
        if (len == 1) {
            return 0;
        }
        boolean boo = true;
        while (i < len && j < len) {
            if (nums[j] - nums[i] > k) {
                i++;
            } else if (nums[j] - nums[i] < k) {
                j++;
            } else {
                sum++;
                int t = nums[i];
                while (t == nums[i]) {
                    i++;
                    if (i>=len){
                        break;
                    }
                }
            }
            if (i >= j) {
                j = i + 1;
            }
        }
        return sum;

    }
    public int findPairsS(int[] nums, int k) {
        Set<Integer> P=new HashSet<>();
        Set<Integer> Q=new HashSet<>();
        for(int num :nums){
            if(P.contains(num-k)){
                Q.add(num-k);
            }
            if(P.contains(num+k)){
                Q.add(num);
            }
            P.add(num);
        }
        return Q.size();
    }


    @Test
    public void Test() {

        int[] ints = {1, 1, 1, 1, 1};
        int k = 0;
        int r = findPairsS(ints, k);
        System.out.println(r);

    }
}
